package com.xdcplus.web.service.wip.impl;

import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.common.pojo.vo.WipMarkingElSummaryVO;
import com.xdcplus.web.mapper.WipMarkingPackInfoMapper;
import com.xdcplus.web.common.pojo.entity.WipMarkingPackInfo;
import com.xdcplus.web.common.pojo.dto.WipMarkingPackInfoDTO;
import com.xdcplus.web.common.pojo.dto.WipMarkingPackInfoFilterDTO;
import com.xdcplus.web.common.pojo.vo.WipMarkingPackInfoVO;
import com.xdcplus.web.common.pojo.query.WipMarkingPackInfoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.wip.WipMarkingPackInfoService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

/**
 * (WipMarkingPackInfo)表服务实现类
 *
 * @author bullion.yan
 * @since 2024-11-28 18:34:28
 */
@Slf4j
@Service("wipMarkingPackInfoService")
public class WipMarkingPackInfoServiceImpl extends BaseServiceImpl<WipMarkingPackInfoMapper, WipMarkingPackInfo, WipMarkingPackInfo, WipMarkingPackInfoVO> implements WipMarkingPackInfoService {

    @Autowired
    protected WipMarkingPackInfoMapper wipMarkingPackInfoMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean saveWipMarkingPackInfo(WipMarkingPackInfoDTO wipMarkingPackInfoDTO) {

        WipMarkingPackInfo wipMarkingPackInfo = new WipMarkingPackInfo();
        BeanUtil.copyProperties(wipMarkingPackInfoDTO, wipMarkingPackInfo);
        wipMarkingPackInfo.setCreatedTime(new Timestamp(DateUtil.current()));
        
        return this.save(wipMarkingPackInfo);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean updateWipMarkingPackInfo(WipMarkingPackInfoDTO wipMarkingPackInfoDTO) {

        WipMarkingPackInfo wipMarkingPackInfo = this.getById(wipMarkingPackInfoDTO.getId());
        if (ObjectUtil.isNull(wipMarkingPackInfo)) {
            log.error("updateWipMarkingPackInfo() The WipMarkingPackInfo does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(wipMarkingPackInfoDTO, wipMarkingPackInfo);
        wipMarkingPackInfo.setUpdatedUser(wipMarkingPackInfoDTO.getUpdatedUser());
        wipMarkingPackInfo.setUpdatedTime(new Timestamp(DateUtil.current()));

        return this.updateById(wipMarkingPackInfo);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<WipMarkingPackInfo> wipMarkingPackInfoList) {

        wipMarkingPackInfoList.forEach(wipMarkingPackInfo -> {
            WipMarkingPackInfo wipMarkingPackInfoParam = new WipMarkingPackInfo();
            wipMarkingPackInfoParam.setId(wipMarkingPackInfo.getId());
            if (ObjectUtil.isNotNull(wipMarkingPackInfo.getId())) {
                wipMarkingPackInfo.setId(wipMarkingPackInfo.getId());
                wipMarkingPackInfo.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<WipMarkingPackInfo> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(WipMarkingPackInfo::getId, wipMarkingPackInfo.getId());
                update(wipMarkingPackInfo, lambdaUpdate);
            } else {
                wipMarkingPackInfo.setCreatedTime(new Timestamp(DateUtil.current()));
                save(wipMarkingPackInfo);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<WipMarkingPackInfoDTO> wipMarkingPackInfoDTOList) {

        List<WipMarkingPackInfo> wipMarkingPackInfoList = BeanUtils.copyProperties(wipMarkingPackInfoDTOList, WipMarkingPackInfo.class);
        return saveOrUpdateBatch(wipMarkingPackInfoList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingPackInfoLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        WipMarkingPackInfo wipMarkingPackInfo = this.getById(id);

        if (ObjectUtil.isNull(wipMarkingPackInfo)) {
            log.error("deleteWipMarkingPackInfo() The WipMarkingPackInfo does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        return this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Boolean deleteWipMarkingPackInfoByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			WipMarkingPackInfo wipMarkingPackInfo = this.getById(id);
			if (ObjectUtil.isNull(wipMarkingPackInfo)) {
				log.error("deleteWipMarkingPackInfo() The WipMarkingPackInfo does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});
		
        return this.removeByIds(idList);
    }

    private List<WipMarkingPackInfo> queryWipMarkingPackInfoList(WipMarkingPackInfoFilterDTO wipMarkingPackInfoFilterDTO) {
        WipMarkingPackInfoQuery wipMarkingPackInfoQuery = BeanUtil.copyProperties(wipMarkingPackInfoFilterDTO, WipMarkingPackInfoQuery.class);

        return wipMarkingPackInfoMapper.queryWipMarkingPackInfo(wipMarkingPackInfoQuery);
    }

    @Override
    public List<WipMarkingPackInfoVO> queryWipMarkingPackInfoVOList(WipMarkingPackInfoFilterDTO wipMarkingPackInfoFilterDTO) {
        return this.objectConversion(queryWipMarkingPackInfoList(wipMarkingPackInfoFilterDTO));
    }

    @Override
    public PageVO<WipMarkingPackInfoVO> queryWipMarkingPackInfo(WipMarkingPackInfoFilterDTO wipMarkingPackInfoFilterDTO) {
        PageVO<WipMarkingPackInfoVO> pageVO = new PageVO<>();

        if (wipMarkingPackInfoFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(wipMarkingPackInfoFilterDTO.getCurrentPage(), wipMarkingPackInfoFilterDTO.getPageSize(),
                    wipMarkingPackInfoFilterDTO.getOrderType(), wipMarkingPackInfoFilterDTO.getOrderField());
        }

        List<WipMarkingPackInfo> wipMarkingPackInfoList = queryWipMarkingPackInfoList(wipMarkingPackInfoFilterDTO);

        PageInfo<WipMarkingPackInfo> pageInfo = new PageInfo<>(wipMarkingPackInfoList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(wipMarkingPackInfoList));

        return pageVO;
    }

    @Override
    public WipMarkingPackInfoVO queryWipMarkingPackInfoById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }

    @Override
    public List<WipMarkingPackInfoVO> queryByMarkingId(Long markingId) {
        return this.objectConversion(wipMarkingPackInfoMapper.queryByMarkingId(markingId));
    }
}
